package com.google.code.synchronized_player;

import java.io.File;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ListView;

public class MainActivity extends Activity {
	ListView playlist;
	PlayListAdapter adapter;
	ArrayList<Track> filenames = new ArrayList<Track>();
    /** Called when the activity is first created. */
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        SharedPreferences sp = this.getApplicationContext().getSharedPreferences("preferences", MODE_PRIVATE);
        String directoryName = sp.getString("directoryName", "/");
        if(!directoryName.equals("")) {
        	File directory = new File(directoryName);
        	if(directory.isDirectory()) {
        		File[] files = directory.listFiles();
        		for(File f: files) {
        			filenames.add(new Track(f.getName()));
        		}
        	}
        }
        playlist = (ListView)this.findViewById(R.id.playlist);
        adapter = new PlayListAdapter(this, filenames);
        playlist.setAdapter(adapter);
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.layout.main_menu, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.settings:
            startActivity(new Intent(this.getApplicationContext(), SettingsActivity.class));
            return true;
        case R.id.exit:
        	System.exit(0);
        default:
            return super.onOptionsItemSelected(item);
        }
    }
}